package com.lemon.tuvi2015.view;

import java.util.ArrayList;

import org.json.JSONException;

import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.lemon.tuvi2015.R;
import com.lemon.tuvi2015.util.JsonParser;

public class AdmobView extends RelativeLayout {
	// private LayoutInflater layoutInflater;
	// RelativeLayout mActionLayout;
	Context context;
	private AdView adView;
	private static final String LOG_TAG = "AdmobView";
	String admob = "";

	public AdmobView(final Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		admob = context.getString(R.string.admob_id);
		initAd(admob);
		new AsyncTaskAdmob().execute("");
	}

	void initAd(String admobid) {
		adView = new AdView(context);
		adView.setAdSize(AdSize.BANNER);
		adView.setAdUnitId(admobid);
		adView.setAdListener(new AdListener() {
			/**
			 * Called when an ad is clicked and about to return to the
			 * application.
			 */
			@Override
			public void onAdClosed() {
				Log.d(LOG_TAG, "onAdClosed");
			}

			/** Called when an ad failed to load. */
			@Override
			public void onAdFailedToLoad(int error) {
				String message = "onAdFailedToLoad: " + getErrorReason(error);
				Log.d(LOG_TAG, message);
			}

			/**
			 * Called when an ad is clicked and going to start a new Activity
			 * that will leave the application (e.g. breaking out to the Browser
			 * or Maps application).
			 */
			@Override
			public void onAdLeftApplication() {
				Log.d(LOG_TAG, "onAdLeftApplication");
			}

			/**
			 * Called when an Activity is created in front of the app (e.g. an
			 * interstitial is shown, or an ad is clicked and launches a new
			 * Activity).
			 */
			@Override
			public void onAdOpened() {
				Log.d(LOG_TAG, "onAdOpened");
			}

			/** Called when an ad is loaded. */
			@Override
			public void onAdLoaded() {
				Log.d(LOG_TAG, "onAdLoaded");
				setVisibility(View.VISIBLE);
				requestLayout();
			}
		});
		setGravity(Gravity.CENTER);
		addView(adView);
		RequestAdmob();
	}

	void RequestAdmob() {
		AdRequest adRequest = new AdRequest.Builder().build();
		adView.loadAd(adRequest);
	}

	/** Gets a string error reason from an error code. */
	private String getErrorReason(int errorCode) {
		String errorReason = "";
		switch (errorCode) {
		case AdRequest.ERROR_CODE_INTERNAL_ERROR:
			errorReason = "Internal error";
			break;
		case AdRequest.ERROR_CODE_INVALID_REQUEST:
			errorReason = "Invalid request";
			break;
		case AdRequest.ERROR_CODE_NETWORK_ERROR:
			errorReason = "Network Error";
			break;
		case AdRequest.ERROR_CODE_NO_FILL:
			errorReason = "No fill";
			break;
		}
		return errorReason;
	}

	public class AsyncTaskAdmob extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.getAdmobID();

			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					ArrayList<String> arrayList = JsonParser.readAmobID(result);
					if (arrayList != null) {
						String adm = arrayList.get(0);
						if (!adm.equals(admob)) {
							initAd(arrayList.get(0));
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
